package controllers

import (
	"xmgl/models"

	//"log"
	"net/http"

	"time"

	"github.com/astaxie/beego"
)

/*
运维服务，数据统计
*/
type WwfwSjtjController struct {
	beego.Controller
}

type Api_WwfwSjtjController struct {
	beego.Controller
}

func init() {

}

func (this *WwfwSjtjController) Get() {
	var (
		menu models.Menu_gl
		user models.Sut_user
	)
	if this.GetSession("USER") == nil {
		//没有取到session值，页面跳转
		http.Redirect(this.Ctx.ResponseWriter, this.Ctx.Request, "/login", http.StatusFound)
	}
	tmp, ok := this.GetSession("USER").(models.Sut_user) //注意类型断言
	if ok {
		user = tmp
	}
	//log.Println("从session取用户名：", tmp.User_name, "ID：", tmp.User_id)
	//sess := this.StartSession()
	//sess.Set("USER", user)
	//log.Println("从session取用户名：", sess.Get("USER"))

	this.Data["WEBSITE"] = "运维管理/数据统计与分析"
	this.Data["MENU_LEFT"] = menu.Get_left_menu()
	this.Data["MENU_RIGHT"] = menu.Get_right_menu()
	this.Data["USER_NAME"] = user.User_name

	this.Layout = "layout.html"
	this.TplName = "wwfwSjtj.html"
	this.LayoutSections = make(map[string]string)
	this.LayoutSections["HtmlHeader"] = "html_header.html"
}

///api/v1/wwfw/ywsjtj/test返回测试数据
func (this *Api_WwfwSjtjController) Get() {
	var (
		C1         models.Sut_EChart     //柱图的数据结构
		pie1       models.Sut_EChart_Pie //饼图的数据结构
		start_date string
		end_date   string
		err        error
	)
	dataType := this.GetString(":datatype") //取得API调用的子参数
	//start_date := this.GetString("start_date")      //取得API调用时，AJAX传来的数据及值
	//end_date := this.GetString("end_date")          //取得API调用时，AJAX传来的数据及值
	start_date_unix, err := this.GetInt64("start_date") //取得API调用时，AJAX传来的UNIX时间值
	if err != nil {
		this.Data["json"] = C1
		this.ServeJSON()
	}
	end_date_unix, err := this.GetInt64("end_date") //取得API调用时，AJAX传来的UNIX时间值
	if err != nil {
		this.Data["json"] = C1
		this.ServeJSON()
	}
	t1 := time.Unix(start_date_unix, 0) //将unix时间转成Time结构
	t2 := time.Unix(end_date_unix, 0)   //将unix时间转成Time结构
	//t1, err := time.Parse("2006-01-02", start_date) //开始时间改成00:00小时，并转成UNIX时间
	//t2, err := time.Parse("2006-01-02", end_date)   //结束时间改成00:00小时，并转成UNIX时间
	subhour, _ := time.ParseDuration("-8h")
	t1 = t1.Add(subhour) //开始时间减去它已的小时数
	addhour, _ := time.ParseDuration("+15h")
	addmin, _ := time.ParseDuration("+59m")
	t2 = t2.Add(addhour) //结束时间增加小时
	t2 = t2.Add(addmin)  //结束时间增加分钟
	start_date = t1.Format("2006-01-02 15:04:05")
	end_date = t2.Format("2006-01-02 15:04:05")
	/*
		log.Println("Api_WwfwSjtjController，datatype:", dataType)
		log.Println("start_date:", start_date_unix)
		log.Println("t1 unix:", t1.Unix(), "t1:", t1.String(), "，t2:", t2.String(), "err:", err)
		log.Println("start修改为凌晨后：", t1.Format("2006-01-02 15:04:05"), "end修改为24点后：", t2.Format("2006-01-02 15:04:05"))
	*/
	if dataType == "test" {
		C1.TestData()
		this.Data["json"] = C1
	} else if dataType == "GZFLHZ" {
		C1.GetGZFLHZ(start_date, end_date)
		this.Data["json"] = C1
	} else if dataType == "GZSBHZ" {
		C1.GetGZSBHZ(start_date, end_date)
		this.Data["json"] = C1
	} else if dataType == "JSRHZ" {
		C1.GetJSRHZ(start_date, end_date)
		this.Data["json"] = C1
	} else if dataType == "CLFSHZ" {
		pie1.GetCLFSHZ(start_date, end_date)
		this.Data["json"] = pie1
	}
	this.ServeJSON()
}
